<?php
/**
 * Created by PhpStorm.
 * User: MR_THONG
 * Date: 2/22/2015
 * Time: 8:14 AM
 */    App::uses('AppController', 'Controller');
class AdminController extends AppController {
    //public $helpers = array('Html', 'Form', 'Wysiwyg.Tinymce'=> array('editor' => 'Ck'));
    public function admin_useredit($id){
        $this->loadModel('User');
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Thông tin không tồn tại', true), 'default', array('class' => 'error'));
            $this->redirect(array(
                'controller' => 'Admin',
                'action' => 'userindex'));
        }
        if (!empty($this->data)) {
            if ($this->User->save($this->data)) {
                $this->Session->setFlash(__d('croogo', 'Lưu thông tin thành công'), 'default', array('class' => 'success'));
                $this->redirect(array(
                    'controller' => 'Admin',
                    'action' => 'userindex'
                ));
            } else {
                $this->Session->setFlash(__d('croogo', 'Lưu thông tin thất bại. Vui lòng thử lại.'), 'default', array('class' => 'error'));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->User->read(null, $id);
        }
        $this->loadModel('Role');
        $groups = $this->Role->find('list');
        $this->set(compact('groups'));
    }
    public function admin_userindex(){
        $this->set('title_for_layout', 'Quản lý thành viên');
        $this->loadModel('User');
        $data = $this->User->find('all');
        $this->set('listUser',$data);
    }
    function admin_useradd() {
        $this->loadModel('User');
        $user = $this->Session->read('User');
        $this->set('title_for_layout', 'Thêm mới thành viên');
        if (!empty($this->request->data)) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__d('croogo', 'Lưu thông tin thành công'), 'default', array('class' => 'success'));
                $this->redirect(array(
                    'controller' => 'Admin',
                    'action' => 'userindex'));
            } else {
                $this->Session->setFlash(__d('croogo', 'Lưu thông tin thất bại. Vui lòng thử lại.'), 'default', array('class' => 'error'));
                unset($this->request->data['User']['password']);
            }
        } else {
            $this->request->data['User']['group_id'] = 3; // default Role: Registered
        }
        $this->loadModel('Role');
        //$groups = $this->Role->find('list');
        $groups = $this->Role->find('list');
        $this->set(compact('groups'));
    }

    public function admin_userdelete($id = null) {
        $this->loadModel('User');
        if (!$id) {
            $this->Session->setFlash(__('Thông tin không tồn tại', true), 'default', array('class' => 'error'));
            $this->redirect(array('action' => 'userindex'));
        }
        else{
            $user = $this->Session->read('User');
            if ($user[0]['User']['id'] == $id ) {
                $this->Session->setFlash(__('Bạn không thể xoá bản thân', true), 'default', array('class' => 'error'));
                $this->redirect(array('action' => 'userindex'));
            }
            else{
                if ($this->User->delete($id)) {
                    $this->Session->setFlash(__d('croogo', 'Xoá thông tin thành công'), 'default', array('class' => 'success'));
                    $this->redirect(array('action' => 'userindex'));
                }
            }
        }
    }

    function admin_tourindex(){
        $this->loadModel('Tour');
        $dataTour = $this->Tour->find('all');

        $this->set('listTour',$dataTour);
    }

    function admin_tourdelete($id = null){
        $this->loadModel('Tour');
        $this->Tour->delete($id);
        $this->redirect(array(
            'controller' => 'Admin',
            'action' => 'tourindex'
        ));
    }

    function admin_touradd(){
        $this->loadModel('Tour');
        $dataRequest = $this->request->data;

        $this->loadModel('TourKind');
        $tourkindgroups = $this->TourKind->find('list');
        $this->set(compact('tourkindgroups'));

        if($dataRequest){
            $this->Tour->create();
            if($this->Tour->save($dataRequest)){
                //Upload Image
                //            Kiem tra images neu co edit img
                if (($dataRequest['Tour']['imageURL']) && $dataRequest['Tour']['Isnew'] == 1) {
                    $path = WWW_ROOT;
                    //ten file save o forder tam thoi.
                    $sound = $path.'nghia'.DS.'Temp'.DS.$this->request->data['Tour']['imageURL'];

                    if (file_exists($sound)) {
                        //duong link se copy hinh den
                        $target_dir = $path.'img';
                        if (!file_exists($target_dir)) { mkdir($target_dir, 0755, true); }
                        //kiem tra neu co roi thi de` len
                        if (file_exists($target_dir.DS.$dataRequest['Tour']['imageURL'])) {
                            $des = time()."_".$this->imageURL['Tour']['imageURL'];
                            rename($sound, $target_dir.DS.$des);
                            //                        $this->Product->saveField("images",$des);
                        } else {
                            //                        neu chua co thi copy luon
                            rename($sound, $target_dir.DS.$dataRequest['Tour']['imageURL']);
                        }
                    }
                }
            }
            $this->redirect(array(
                'controller' => 'Admin',
                'action' => 'tourindex'
            ));
        }
    }

    function admin_toursetupindex(){
        $this->loadModel('CustomerTour');
        $customerTour = $this->CustomerTour->find('all');
        $this->set('customertour',$customerTour);
    }

    function admin_customersupportindex(){
        $this->loadModel('CustomerSupport');
        $customerSupport = $this->CustomerSupport->find('all');
        $this->set('customerSupport',$customerSupport);
    }

    function admin_customercontactindex(){
        $this->loadModel('CustomerContact');
        $customerContact = $this->CustomerContact->find('all');
        $this->set('customerContact',$customerContact);
    }

    function admin_touredit($i){
        $this->loadModel('Tour');
        $condition = array('conditions'=> array('Tour.id' => $i)) ;
        $dataTour = $this->Tour->find('all',$condition);

        $this->loadModel('TourKind');
        $tourkindgroups = $this->TourKind->find('list');
        $this->set(compact('tourkindgroups'));

        $this->set('la',$dataTour[0]);

        // request data and save
        $temp = $this->request->data;
        if($temp){
            $temp['Tour']['tour_kind_id'] = $temp['Tour']['tourkindgroups_id'];
            //Upload Image
            //            Kiem tra images neu co edit img
                                if (($temp['Tour']['imageURL']) && $temp['Tour']['Isnew'] == 1) {
                                    $path = WWW_ROOT;
                                    //ten file save o forder tam thoi.
                                    $sound = $path.'nghia'.DS.'Temp'.DS.$this->request->data['Tour']['imageURL'];
                                    if (file_exists($sound)) {
                                        //duong link se copy hinh den
                                        $target_dir = $path.'img';
                                        if (!file_exists($target_dir)) { mkdir($target_dir, 0755, true); }
                                        //kiem tra neu co roi thi de` len
                                        if (file_exists($target_dir.DS.$temp['Tour']['imageURL'])) {
                                            $des = time()."_".$this->imageURL['Tour']['imageURL'];
                                            rename($sound, $target_dir.DS.$des);
                    //                        $this->Product->saveField("images",$des);
                                        } else {
                    //                        neu chua co thi copy luon
                                            rename($sound, $target_dir.DS.$temp['Tour']['imageURL']);
                                        }
                                    }
                                }

            $this->Tour->save($temp);
            $this->redirect(array(
                'controller' => 'Admin',
                'action' => 'tourindex'
            ));
        }
    }

    function admin_tourkindindex() {
        $this->loadModel('TourKind');
        $dataTourKind = $this->TourKind->find('all');
        $this->set('dataTourKind',$dataTourKind);
    }

    function admin_tourkindadd() {

        $this->loadModel('TourKind');

        $temp = $this->request->data;
        if($temp) {
            if ($this->TourKind->save($temp)) {
                $this->redirect(array(
                    'controller' => 'Admin',
                    'action' => 'tourkindindex'
                ));
            };
        }
    }


    function admin_tourkindedit($i) {
        $this->loadModel('TourKind');
        $tourKind = $this->TourKind->findById($i);
        $this->set('tourKind',$tourKind);
        $temp = $this->request->data;
        if($temp) {
            if ($this->TourKind->save($temp)) {
                $this->redirect(array(
                    'controller' => 'Admin',
                    'action' => 'tourkindindex'
                ));
            };
        }
    }

    function admin_articleindex(){
        $this->loadModel('Artical');
        $dataArticle = $this->Artical->find('all');
        $this->set('listArticle',$dataArticle);
    }
    public function admin_articaledit($i){
        $this->loadModel('Artical');
        $condition = array('conditions'=> array('Artical.id' => $i)) ;
        $dataArticle = $this->Artical->find('all',$condition);
        $this->set('listArtical',$dataArticle);

        // request data and save
        $temp = $this->request->data;
        if($temp){
            $this->Artical->save($temp);
            $this->redirect(array(
                'controller' => 'Admin',
                'action' => 'articleindex'
            ));
        }
    }

    public function admin_supportserviceindex() {
        $this->loadModel('SupportService');
        $listSupportServices = $this->SupportService->find('all');
        $this->set('listSupportServices',$listSupportServices);
        //var_dump($listSupportServices); die;
    }

    public function admin_supportserviceedit($id) {

        $this->loadModel('SupportService');
        $supportService = $this->SupportService->findById($id);
        $this->set('supportService',$supportService);

        $temp = $this->request->data;

        if($temp) {
            if ($this->SupportService->save($temp)) {
                $this->redirect(array(
                    'controller' => 'Admin',
                    'action' => 'supportserviceindex'
                ));
            };
        }
    }

    public function admin_supportserviceadd() {

        $this->loadModel('SupportService');

        $temp = $this->request->data;
        if($temp) {
            if ($this->SupportService->save($temp)) {
                $this->redirect(array(
                    'controller' => 'Admin',
                    'action' => 'supportserviceindex'
                ));
            };
        }
    }

    public function admin_supportservicedelete($id) {
        $this->loadModel('SupportService');
        $this->SupportService->delete($id);
        $this->redirect(array(
            'controller' => 'Admin',
            'action' => 'supportserviceindex'
        ));
    }
}
?>